<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 *
 * @package      925CMS
 * @subpackage  Categories
 * @category      Module
 */

class Admin extends Admin_Controller {
    /**
     * The id of post
     * @access protected
     * @var int
     */
    protected $id = 0;
    /**
     * Array that contains the validation rules
     * @access protected
     * @var array
     */
    protected $validation_rules = array(
        array(
            'field' => 'title',
            'label' => 'lang:blog_title_label',
            'rules' => 'trim|htmlspecialchars|required|max_length[250]'
        ) ,
        array(
            'field' => 'short_title',
            'label' => 'lang:blog_title_label',
            'rules' => 'trim|htmlspecialchars|required|max_length[100]|callback__check_title'
        ) ,
        array(
            'field' => 'slug',
            'label' => 'lang:blog_slug_label',
            'rules' => 'trim|required|alpha_dot_dash|max_length[100]|callback__check_slug'
        ) ,
        array(
            'field' => 'category_id',
            'label' => 'lang:blog_category_label',
            'rules' => 'trim|numeric'
        ) ,
        array(
            'field' => 'intro',
            'label' => 'lang:blog_intro_label',
            'rules' => 'trim|required'
        ) ,
        array(
            'field' => 'body',
            'label' => 'lang:blog_content_label',
            'rules' => 'trim|required'
        ) ,
        array(
            'field' => 'status',
            'label' => 'lang:blog_status_label',
            'rules' => 'trim|alpha'
        ) ,
        array(
            'field' => 'created',
            'label' => 'lang:blog_date_label',
            'rules' => 'trim|required'
        ) ,
        array(
            'field' => 'created_hour',
            'label' => 'lang:blog_created_hour',
            'rules' => 'trim|numeric|required'
        ) ,
        array(
            'field' => 'created_minute',
            'label' => 'lang:blog_created_minute',
            'rules' => 'trim|numeric|required'
        ) ,
        array(
            'field' => 'comments_enabled',
            'label' => 'lang:blog_comments_enabled_label',
            'rules' => 'trim|numeric'
        ) ,
        array(
            'field' => 'thumbnail_id',
            'label' => 'lang:thumbnail_label',
            'rules' => 'trim|numeric'
        )
    );
    /**
     * The constructor
     * @access public
     * @return \Admin
     */
    public 
    function __construct() {

        parent::__construct();
        $this->load->model('deal_m');
        $this->load->model('blog_categories_m');
        $this->lang->load('groupon');
        $this->lang->load('categories');
        $this->load->helper('number');

        // Date ranges for select boxes
        $this->data->hours = array_combine($hours = range(0, 23) , $hours);
        $this->data->minutes = array_combine($minutes = range(0, 59) , $minutes);
        $this->data->categories = $this->build_tree_array(array(
            'type' => 'blog_categories'
        ));

        //        if ($categories = $this->blog_categories_m->order_by('title')->get_all()) {
        
        //            foreach ($categories as $category)

        
        //            {

        
        //                $this->data->categories[$category->id] = $category->title;

        
        //            }

        
        //        }

        $this->template->append_metadata(css('groupon.css', 'groupon'))->set_partial('shortcuts', 'admin/partials/shortcuts');
    }
    /**
     * Show all created blog posts
     * @access public
     * @return void
     */
    public 
    function index() {


        //set the base/default where clause
        $base_where = array(
            'show_future' => TRUE,
            'status' => 'all'
        );

        //add post values to base_where if f_module is posted
        $base_where = $this->input->post('f_category') ? $base_where + array(
            'category' => $this->input->post('f_category')
        ) : $base_where;
        $base_where['status'] = $this->input->post('f_status') ? $this->input->post('f_status') : $base_where['status'];
        $base_where = $this->input->post('f_keywords') ? $base_where + array(
            'keywords' => $this->input->post('f_keywords')
        ) : $base_where;

        // Create pagination links
        $total_rows = $this->deal_m->count_by($base_where);
        $pagination = create_pagination('admin/blog/index', $total_rows);

        // Using this data, get the relevant results
        $deals = $this->deal_m->limit($pagination['limit'])->get_many_by($base_where);
        foreach ($deals as & $deal) {
            $deal->author = $this->ion_auth->get_user($deal->author_id);
        }

        //do we need to unset the layout because the request is ajax?
        $this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';
        $this->template->title($this->module_details['name'])->set_partial('filters', 'admin/partials/filters')->append_metadata(js('admin/filter.js'))->set('pagination', $pagination)->set('deals', $deals)->build('admin/index', $this->data);
    }
    /**
     * Create new post
     * @access public
     * @return void
     */
    public 
    function create() {

        $this->load->library('form_validation');
        $this->form_validation->set_rules($this->validation_rules);
        
        if ($this->input->post('created')) {
            $created = strtotime(sprintf('%s %s:%s', $this->input->post('created') , $this->input->post('created_hour') , $this->input->post('created_minute')));
        } else {
            $created = now();
        }
        
        if ($this->form_validation->run()) {

            // They are trying to put this live
            
            if ($this->input->post('status') == 'live') {
                role_or_die('blog', 'put_live');
            }
            $id = $this->deal_m->insert(array(
                'title' => $this->input->post('title') ,
                'slug' => $this->input->post('slug') ,
                'category_id' => $this->input->post('category_id') ,
                'intro' => $this->input->post('intro') ,
                'body' => $this->input->post('body') ,
                'status' => $this->input->post('status') ,
                'created' => $created,
                'comments_enabled' => $this->input->post('comments_enabled') ,
                'author_id' => $this->user->id,
                'thumbnail_id' => $this->input->post('thumbnail_id') ,
            ));
            
            if ($id) {
                $this->pyrocache->delete_all('deal_m');
                $this->session->set_flashdata('success', sprintf($this->lang->line('blog_post_add_success') , $this->input->post('title')));
            } else {
                $this->session->set_flashdata('error', $this->lang->line('blog_post_add_error'));
            }

            // Redirect back to the form or main page
            $this->input->post('btnAction') == 'save_exit' ? redirect('admin/blog') : redirect('admin/blog/edit/' . $id);
        } else {
            /** @var $post object*/

            // Go through all the known fields and get the post values
            foreach ($this->validation_rules as $key => $field) {
                $deal->$field['field'] = set_value($field['field']);
            }
            $deal->created = $created;
        }
        $this->template->title($this->module_details['name'], lang('blog_create_title'))->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))->append_metadata(js('groupon_form.js', 'blog'))->set('post', $post)->build('admin/form');
    }
    /**
     * Edit blog post
     * @access public
     * @param int $id the ID of the blog post to edit
     * @return void
     */
    public 
    function edit($id = 0) {

        $id OR redirect('admin/groupon');
        $this->load->library('form_validation');
        $this->form_validation->set_rules($this->validation_rules);
        $deal = $this->deal_m->get($id);
        $deal->author = $this->ion_auth->get_user($deal->author_id);

        // If we have a useful date, use it
        
        if ($this->input->post('created')) {
            $created = strtotime(sprintf('%s %s:%s', $this->input->post('created') , $this->input->post('created_hour') , $this->input->post('created_minute')));
        } else {
            $created = $deal->created;
        }
        $this->id = $deal->id;
        
        if ($this->form_validation->run()) {

            // They are trying to put this live
            
            if ($deal->status != 'live' and $this->input->post('status') == 'live') {
                role_or_die('groupon', 'put_live');
            }
            $author_id = empty($deal->author) ? $this->user->id : $deal->author_id;
            $result = $this->deal_m->update($id, array(
                'title' => $this->input->post('title') ,
                'slug' => $this->input->post('slug') ,
                'category_id' => $this->input->post('category_id') ,
                'intro' => $this->input->post('intro') ,
                'body' => $this->input->post('body') ,
                'status' => $this->input->post('status') ,
                'created' => $created,
                'comments_enabled' => $this->input->post('comments_enabled') ,
                'author_id' => $author_id,
                'thumbnail_id' => $this->input->post('thumbnail_id') ,
            ));
            
            if ($result) {
                $this->session->set_flashdata(array(
                    'success' => sprintf($this->lang->line('groupon_edit_success') , $this->input->post('title'))
                ));

                // The twitter module is here, and enabled!
                
                //				if ($this->settings->item('twitter_blog') == 1 && ($deal->status != 'live' && $this->input->post('status') == 'live'))

                
                //				{

                
                //					$url = shorten_url('blog/'.$date[2].'/'.str_pad($date[1], 2, '0', STR_PAD_LEFT).'/'.url_title($this->input->post('title')));

                
                //					$this->load->model('twitter/twitter_m');

                
                //					if ( ! $this->twitter_m->update(sprintf($this->lang->line('blog_twitter_posted'), $this->input->post('title'), $url)))

                
                //					{

                
                //						$this->session->set_flashdata('error', lang('blog_twitter_error') . ": " . $this->twitter->last_error['error']);

                
                //					}

                
                //				}

                
            } else {
                $this->session->set_flashdata(array(
                    'error' => $this->lang->line('blog_edit_error')
                ));
            }

            // Redirect back to the form or main page
            $this->input->post('btnAction') == 'save_exit' ? redirect('admin/blog') : redirect('admin/blog/edit/' . $id);
        }

        // Go through all the known fields and get the post values
        foreach (array_keys($this->validation_rules) as $field) {
            
            if (isset($_POST[$field])) {
                $deal->$field = $this->form_validation->$field;
            }
        }
        $deal->created = $created;

        // Load WYSIWYG editor
        $this->template->title($this->module_details['name'], sprintf(lang('blog_edit_title') , $deal->title))->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))->append_metadata(js('blog_form.js', 'blog'))->set('deal', $deal)->build('admin/form');
    }
    /**
     * Preview blog post
     * @access public
     * @param int $id the ID of the blog post to preview
     * @return void
     */
    public 
    function preview($id = 0) {

        $post = $this->deal_m->get($id);
        $this->template->set_layout('modal', 'admin')->set('post', $post)->build('admin/preview');
    }
    /**
     * Helper method to determine what to do with selected items from form post
     * @access public
     * @return void
     */
    public 
    function action() {

        
        switch ($this->input->post('btnAction')) {
            case 'publish':
                role_or_die('blog', 'put_live');
                $this->publish();
            break;
            case 'delete':
                role_or_die('blog', 'delete_live');
                $this->delete();
            break;
            default:
                redirect('admin/blog');
            break;
        }
    }
    /**
     * Publish blog post
     * @access public
     * @param int $id the ID of the blog post to make public
     * @return void
     */
    public 
    function publish($id = 0) {

        role_or_die('blog', 'put_live');

        // Publish one
        $ids = ($id) ? array(
            $id
        ) : $this->input->post('action_to');
        
        if (!empty($ids)) {

            // Go through the array of slugs to publish
            $post_titles = array();
            foreach ($ids as $id) {

                // Get the current page so we can grab the id too
                
                if ($post = $this->deal_m->get($id)) {
                    $this->deal_m->publish($id);

                    // Wipe cache for this model, the content has changed
                    $this->pyrocache->delete('deal_m');
                    $post_titles[] = $deal->title;
                }
            }
        }

        // Some posts have been published
        
        if (!empty($post_titles)) {

            // Only publishing one post
            
            if (count($post_titles) == 1) {
                $this->session->set_flashdata('success', sprintf($this->lang->line('blog_publish_success') , $post_titles[0]));
            }

            // Publishing multiple posts
            else {
                $this->session->set_flashdata('success', sprintf($this->lang->line('deal_mass_publish_success') , implode('", "', $post_titles)));
            }
        }

        // For some reason, none of them were published
        else {
            $this->session->set_flashdata('notice', $this->lang->line('blog_publish_error'));
        }
        redirect('admin/blog');
    }
    /**
     * Delete blog post
     * @access public
     * @param int $id the ID of the blog post to delete
     * @return void
     */
    public 
    function delete($id = 0) {


        // Delete one
        $ids = ($id) ? array(
            $id
        ) : $this->input->post('action_to');

        // Go through the array of slugs to delete
        
        if (!empty($ids)) {
            $post_titles = array();
            foreach ($ids as $id) {

                // Get the current page so we can grab the id too
                
                if ($post = $this->deal_m->get($id)) {
                    $this->deal_m->delete($id);

                    // Wipe cache for this model, the content has changed
                    $this->pyrocache->delete('deal_m');
                    $post_titles[] = $deal->title;
                }
            }
        }

        // Some pages have been deleted
        
        if (!empty($post_titles)) {

            // Only deleting one page
            
            if (count($post_titles) == 1) {
                $this->session->set_flashdata('success', sprintf($this->lang->line('blog_delete_success') , $post_titles[0]));
            }

            // Deleting multiple pages
            else {
                $this->session->set_flashdata('success', sprintf($this->lang->line('deal_mass_delete_success') , implode('", "', $post_titles)));
            }
        }

        // For some reason, none of them were deleted
        else {
            $this->session->set_flashdata('notice', lang('blog_delete_error'));
        }
        redirect('admin/blog');
    }
    /**
     * Callback method that checks the title of an post
     * @access public
     * @param string title The Title to check
     * @return bool
     */
    public 
    function _check_title($title = '') {

        
        if (!$this->deal_m->check_exists('title', $title, $this->id)) {
            $this->form_validation->set_message('_check_title', sprintf(lang('blog_already_exist_error') , lang('blog_title_label')));
            return FALSE;
        }
        return TRUE;
    }
    /**
     * Callback method that checks the slug of an post
     * @access public
     * @param string slug The Slug to check
     * @return bool
     */
    public 
    function _check_slug($slug = '') {

        
        if (!$this->deal_m->check_exists('slug', $slug, $this->id)) {
            $this->form_validation->set_message('_check_slug', sprintf(lang('blog_already_exist_error') , lang('blog_slug_label')));
            return FALSE;
        }
        return TRUE;
    }
    /**
     * method to fetch filtered results for blog list
     * @access public
     * @return void
     */
    public function ajax_filter() {

        $category = $this->input->post('f_category');
        $status = $this->input->post('f_status');
        $keywords = $this->input->post('f_keywords');
        $post_data = array();
        
        if ($status == 'live' OR $status == 'draft') {
            $post_data['status'] = $status;
        }
        
        if ($category != 0) {
            $post_data['category_id'] = $category;
        }

        //keywords, lets explode them out if they exist
        
        if ($keywords) {
            $post_data['keywords'] = $keywords;
        }
        $results = $this->deal_m->search($post_data);

        //set the layout to false and load the view
        $this->template->set_layout(FALSE)->set('blog', $results)->build('admin/index');
    }
}
